package com.example.easyscript.service.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easyscript.entity.BaseEntity;
import io.swagger.models.auth.In;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskLoader {
    public static <T extends BaseEntity> List<T> loadEntityListByTaskId(Integer taskId, ServiceImpl service,Class<T> clazz) {
        QueryWrapper<T> wrapper = new QueryWrapper();
        wrapper.eq("task_id", taskId);
        wrapper.orderByDesc("create_time");
        List list = service.list(wrapper);
        return list;
    }

    public static <T extends BaseEntity> Map<Integer, T> loadEntityMapByTaskId(Integer taskId, ServiceImpl service,Class<T> clazz) {
        List<T> list = loadEntityListByTaskId(taskId, service,clazz);
        Map<Integer, T> map = new HashMap<>();
        for (T o : list) {
            map.put(o.getId(), o);
        }
        return map;
    }

    public static Integer getMaxOrder(ServiceImpl service,Integer taskId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("task_id",taskId);
        List list = service.list(wrapper);
        return list!=null ? list.size() : 0;
    }

    public static <T extends BaseEntity> List<T> getAll(ServiceImpl service,Class<T> clazz){
        QueryWrapper<T> wrapper = new QueryWrapper();
        List list = service.list(wrapper);
        return list;
    }

    public static <T extends BaseEntity> List<T> getByAbstract(ServiceImpl service,Class<T> clazz,String name,Integer value){
        QueryWrapper<T> wrapper = new QueryWrapper();
        wrapper.eq(name, value);
        List list = service.list(wrapper);
        return list;
    }
}
